package com.example.home_app.base.activity

import android.os.Bundle
import android.view.WindowManager
import com.android.frame.mvp.CommonBaseActivity
import com.android.widget.dialog.ConfirmDialog
import com.example.home_app.R
import com.example.home_app.databinding.ActivityAddDeviceBinding
import com.example.home_app.util.SPTool
import com.example.home_app.util.SizeUtil
import com.example.home_app.util.State
import com.example.home_app.util.Utils
import com.gyf.immersionbar.ImmersionBar

/**
 * Created by dell on 2021/8/11.
 */
class AddDeviceActivity: CommonBaseActivity<ActivityAddDeviceBinding>(){

    private var sbid: String? = null
    override fun handleView(savedInstanceState: Bundle?) {
        intView()
    }
    private fun intView(){
        ImmersionBar.with(this).titleBar(binding.toolbar).keyboardEnable(true).init()
        ImmersionBar.with(this).titleBar(binding.toolbar).init()
        setSupportActionBar(binding.toolbar)
        val actionBar = supportActionBar
        actionBar?.setDisplayHomeAsUpEnabled(true)
    }

    override fun initListener() {
        //添加设备
        binding.SetButton.setOnClickListener {
            dateYan()
        }
        //toolbar返回按钮监听
        binding.toolbar.setNavigationOnClickListener { finish() }
    }
    private fun dateYan() {
        sbid = binding.EditTextId.text.toString()
        Utils.counter = 0
        //截取之后的字符
        if (sbid !!.length >= 9 && sbid !== "") {
            when (State.SCENE) {
                "Bathroom" -> {
                    if (State.DeviceIDBathroom?.let { Utils.countStr(it, sbid !!) } !! > 0) {
                        showToast("请勿重复添加设备")
                    } else {
                        if (State.DeviceIDBathroom === "") {
                            State.DeviceIDBathroom = sbid
                        } else {
                            State.DeviceIDBathroom = State.DeviceIDBathroom.toString() + "," + sbid
                        }
                        binding.ImageViewSrc.setImageResource(R.mipmap.ic_right)
                        SPTool.getInstance(this)
                            ?.putString("DeviceIDBathroom", State.DeviceIDBathroom)
                        showSingleDialog()

                    }
                    println(State.DeviceIDBathroom)

                }
                "All" -> {
                    if (State.DeviceIDs?.let { Utils.countStr(it, sbid !!) } !! > 0) {
                        showToast("请勿重复添加设备")
                    } else {
                        if (State.DeviceIDs === "") {
                            State.DeviceIDs = sbid
                        } else {
                            State.DeviceIDs = State.DeviceIDs.toString() + "," + sbid
                        }
                        binding.ImageViewSrc.setImageResource(R.mipmap.ic_right)
                        SPTool.getInstance(this)
                            ?.putString("DeviceIDs", State.DeviceIDs)
                        showSingleDialog()

                    }
                    println(State.DeviceIDs)

                }
                "Home" -> {
                    if (State.DeviceIDHome?.let { Utils.countStr(it, sbid !!) } !!> 0) {
                        showToast("请勿重复添加设备")
                    } else {
                        if (State.DeviceIDHome === "") {
                            State.DeviceIDHome = sbid
                        } else {
                            State.DeviceIDHome = State.DeviceIDHome.toString() + "," + sbid
                        }
                        binding.ImageViewSrc.setImageResource(R.mipmap.ic_right)
                        SPTool.getInstance(this)
                            ?.putString("DeviceIDHome", State.DeviceIDHome)
                        showSingleDialog()
                    }
                    println(State.DeviceIDHome)

                }
                "Kitchen" -> {
                    if (State.DeviceIDKitchen?.let { Utils.countStr(it, sbid !!) } !! > 0) {
                        showToast("请勿重复添加设备")
                    } else {
                        if (State.DeviceIDKitchen === "") {
                            State.DeviceIDKitchen = sbid
                        } else {
                            State.DeviceIDKitchen = State.DeviceIDKitchen.toString() + "," + sbid
                        }
                        binding.ImageViewSrc.setImageResource(R.mipmap.ic_right)
                        SPTool.getInstance(this)
                            ?.putString("DeviceIDKitchen", State.DeviceIDKitchen)
                        showSingleDialog()

                    }
                    println(State.DeviceIDKitchen)

                }
                else -> {
                }
            }
        } else {
            binding.ImageViewSrc.setImageResource(R.mipmap.ic_cuo)
            showToast("ID格式有误")
        }
    }
    //单按钮Dialog
    private fun showSingleDialog() {
        ConfirmDialog.newInstance()
            .setTitle("通知")
            .setContent("添加成功!")
            .setConfirmText("我知道了")
            .setOnConfirmListener {
                it?.dismiss()
                finish()
            }
            .setCancelVisible(false)
            .setDimAmount(0.6f)
            .setOutsideCancelable(false)
            .setViewParams(SizeUtil.dp2px(240f).toInt(), WindowManager.LayoutParams.WRAP_CONTENT)
            .show(supportFragmentManager)
    }

}